#!/bin/sh
#
# 1.2 90/04/12

umask 077

# remove tempfiles upon exit or interruption
trap "rm -f /tmp/getipacct*.$$" 0 1 2

# create awk script to reduce and mark cisco output with date
cat <<'EOF' >/tmp/getipacctawk.$$
BEGIN {
	months["Jan"] = 1;
	months["Feb"] = 2;
	months["Mar"] = 3;
	months["Apr"] = 4;
	months["May"] = 5;
	months["Jun"] = 6;
	months["Jul"] = 7;
	months["Aug"] = 8;
	months["Sep"] = 9;
	months["Oct"] = 10;
	months["Nov"] = 11;
	months["Dec"] = 12;
}

NR==1{
	year = substr($NF, 3, 2);
	month = months[$2];
	day = $3;
	split($4, time, ":");
	hour = time[1];
	minutes = time[2];
	seconds = time[3] + 6;
	
	printf "\nSAMPLE %02d %02d %02d %02d %02d %02d\n\n", year, month, day, hour, minutes, seconds;
}
EOF


# put date in a file
#
date | awk -f /tmp/getipacctawk.$$  >/tmp/getipacctlog.$$

cat <<'EOF' >/tmp/getipacctawk.$$
/ [0-9]*\.[0-9]*\.[0-9]*/ {
	$4 = substr($4, 1, length($4)-1);
	printf "%s %s %d %d\n", $1, $2, $3, $4;
}
EOF

# get cisco data, compress, sort and append to file
#
ETCDIR/ciscotalk <<'EOF'  | awk -f /tmp/getipacctawk.$$ \
| sort -rn +3 >> /tmp/getipacctlog.$$
NORMALPASSWORD
term len 0
show ip acc
ena
ENABLEPASSWORD
clear ip acc
clear ip acc
quit
EOF


# append file to logfile
#
cat /tmp/getipacctlog.$$ >>LOGFILE

cat <<'EOF' >/tmp/getipacctawk.$$
BEGIN {
	cutpct = CUTPCT;
	ntop = NTOP;
	n = 0;
	nby = 0;
	npk = 0;
}

/^SAMPLE/ {
	print $0;
}

/[0-9]*\.[0-9]*\./ {
	if (n<ntop)
	{
		top[n] = $0;
		bytes[n] = $4;
	}
	n++;
	npk += $3;
	nby += $4;
}

END {
	for (i=0; i<ntop; i++)
	{
		if (bytes[i] > nby*cutpct/100)
			print top[i];
	}
	printf "SUM %d %d %d\n\n", n, npk, nby;
}
EOF

awk -f /tmp/getipacctawk.$$ </tmp/getipacctlog.$$ | ETCDIR/nameipacct >>COMPFILE


